﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Account.IService.Entry;
using Identity.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.Service;
using Log.IService;
using Identity.IService;
using Identity.Service;
using Account.Service;
using Account.IService;
using Account.Model.Behavior;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IUserMailService), typeof(UserMailService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class GetUserMailModel : AsyncModelBase, IWriteLog, IValidateIdentity,IGetUserMail
    {
        #region Field

        private const string m_empty_identity = "getmail00";
        private const string m_error_identity = "getmail01";
        private const string m_complete_identity = "getmail02";
        private const string m_error_mail = "getmail03";
        private const string m_complete_mail = "getmail04";

        private const string m_resource_user = "getpassword";
        private const char m_split = ';';

        private const string m_entry = "entry";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(IdentitySource!=null?IdentitySource:m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentitySource { get; set; }
        public string IdentityCode { get; set; }
        public string Source { get; set; }
        public bool IsOnce { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            AccountCode = entry.value;
            this.GetUserMail(m_error_mail,m_complete_mail);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete("error", "identity");
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IGetUserMail
        [System.ComponentModel.DataAnnotations.Editable(false)]
        public string AccountCode { get; set; }

        public void OnGetUserMailComplete(string key, UserMailEntry entry)
        {
            OnComplete(m_entry,entry);
            this.Log(key, entry);
            
        }

        public void OnGetUserMailError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IGetUserMail

        
    }
}